local jubing = fk.CreateSkill{
  name = "plum__jubing",
}

Fk:loadTranslationTable{
  [jubing.name] = "拒兵",
  [":"..jubing.name] = "弃牌阶段开始时，你可以令一名其他角色选择一项：视为对你使用一张【决斗】，或于此阶段结束时弃置X张牌（X为你此阶段弃置牌数）。",

  ["#plum__jubing-choose"] = "拒兵：可以令一名角色选择视为对你使用【决斗】或弃牌",
  ["#plum__jubing-prompt"] = "拒兵：请选择一项",
  ["plum__jubing-discard"] = "于此阶段结束时弃置[等同于 %src 此阶段弃置的牌数]张牌",
  ["plum__jubing-duel"] = "视为对 %src 使用一张【决斗】",

  [jubing.name.."-phase"] = "拒兵",

  ["$plum__jubing1"] = "车马远路疲弊，旌旗乱乎大泽，吴贼必溃。",
  ["$plum__jubing2"] = "异兵虽众，然劳师远来，号令不从，何谈精强？",
}

jubing:addEffect(fk.EventPhaseStart, {
  anim_type = "offensive",
  can_trigger = function (self, event, target, player, data)
    return player:hasSkill(jubing.name) and player == target
    and player.phase == Player.Discard
  end,
  on_cost = function (self, event, target, player, data)
    local room = player.room
    local tos = room:askToChoosePlayers(player, {
      targets = room:getOtherPlayers(player, false),
      min_num = 1,
      max_num = 1,
      skill_name = jubing.name,
      prompt = "#plum__jubing-choose",
      cancelable = true,
    })
    if #tos == 1 then
      event:setCostData(self, tos[1])
      return true
    end
  end,
  on_use = function (self, event, target, player, data)
    local to = event:getCostData(self) ---@type ServerPlayer
    local room = player.room

    local all_choices = { "plum__jubing-discard:"..player.id, "plum__jubing-duel:"..player.id }
    local choices = table.simpleClone(all_choices)
    local duel = Fk:cloneCard("duel")
    duel.skillName = jubing.name
    if not to:canUseTo(duel, player) then
      table.remove(choices, 2)
    end

    local choice = room:askToChoice(to, {
      choices = choices,
      all_choices = all_choices,
      cancelable = false,
      prompt = "#plum__jubing-prompt",
      skill_name = jubing.name,
    })

    if choice == all_choices[2] then
      room:useCard{
        from = to,
        tos = { player },
        card = duel,
      }
    else
      room:setPlayerMark(to, jubing.name, 1)
    end
  end
})

jubing:addEffect(fk.EventPhaseEnd, {
  anim_type = "negative",
  can_trigger = function (self, event, target, player, data)
    return player:getMark(jubing.name) == 1 and target.phase == Player.Discard
  end,
  on_cost = function (self, event, target, player, data)
    player.room:setPlayerMark(player, jubing.name, 0)
    return true
  end,
  on_use = function (self, event, target, player, data)
    local room = player.room
    local n = 0
    room.logic:getEventsOfScope(GameEvent.MoveCards, 999, function (e)
      for _, move in ipairs(e.data) do
        if move.proposer == target and move.moveReason == fk.ReasonDiscard then
          n = n + #move.moveInfo
          return true
        end
      end
    end, Player.HistoryPhase)

    if player:isAlive() then
      n = math.min(n, player:getHandcardNum())
      if n > 0 then
        room:askToDiscard(player, {
          min_num = n,
          max_num = n,
          skill_name = jubing.name,
          cancelable = false,
        })
      end
    end
  end
})

return jubing